﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using DTO;
using BUS;

namespace GUI
{
    public partial class frmThemThietBi : DevComponents.DotNetBar.Metro.MetroForm
    {
        public delegate void ThemMoi(ThietBiDTO dto);
        public event ThemMoi KhiThemMoi = null;

        public static string PathFile = "";
        public static string TenFile = "";

        public frmThemThietBi()
        {
            InitializeComponent();
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void btnDongY_Click(object sender, EventArgs e)
        {
            if (txtTenThietBi.Text == "")
            {
                MessageBoxEx.Show("Bạn chưa nhập tên thiết bị!", "eMobile - Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                txtTenThietBi.Focus();
                return;
            }
            //if (txtGiaNhap.Text == "")
            //{
            //    MessageBoxEx.Show("Bạn chưa nhập giá nhập!", "eMobile - Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            //    txtTenThietBi.Focus();
            //    return;
            //}
            //if (txtGiaBan.Text == "")
            //{
            //    MessageBoxEx.Show("Bạn chưa nhập giá bán!", "eMobile - Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            //    txtTenThietBi.Focus();
            //    return;
            //}
            if (Double.Parse(txtGiaNhap.Text.Replace(",", "")) > Double.Parse(txtGiaBan.Text.Replace(",", "")))
            {
                MessageBoxEx.Show("Giá bán phải cao hơn hoặc bằng giá nhập!", "eMobile - Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                txtGiaBan.Focus();
                return;
            }

            ThietBiDTO dto = LayThongTin();
            if (KhiThemMoi != null)
                KhiThemMoi(dto);
        }

        ThietBiDTO LayThongTin()
        {
            ThietBiDTO tbdto = new ThietBiDTO();
            tbdto.TenThietBi = txtTenThietBi.Text;
            //tbdto.MaLoaiThietBi = (int)cbLoaiThietBi.SelectedValue;
            tbdto.XuatXu = txtXuatXu.Text;
            tbdto.ThongTinMoTa = txtMoTa.Text;
            if (TenFile == "")
                tbdto.HinhAnh = "NoImage.jpg";
            else
                tbdto.HinhAnh = TenFile;
            //tbdto.MaNhaCungCap = (int)cbNhaCungCap.SelectedValue;
            tbdto.IMEI = txtIMEI.Text;
            tbdto.GiaNhap = Double.Parse(txtGiaNhap.Text.Replace(",", ""));
            tbdto.GiaBan = Double.Parse(txtGiaBan.Text.Replace(",", ""));
            tbdto.SoLuongTon = 0;
            return tbdto;
        }

        private void frmThemThietBi_Load(object sender, EventArgs e)
        {
            //LoaiThietBiBUS ltbbus = new LoaiThietBiBUS();
            //List<LoaiThietBiDTO> dsltb = ltbbus.LayDanhSachLoaiThietBi();
            //cbLoaiThietBi.DataSource = dsltb;
            //cbLoaiThietBi.DisplayMember = "TenLoaiThietBi";
            //cbLoaiThietBi.ValueMember = "MaLoaiThietBi";

            //NhaCungCapBUS nccbus = new NhaCungCapBUS();
            //List<NhaCungCapDTO> dsncc = nccbus.LayDanhSachNhaCungCap();
            //cbNhaCungCap.DataSource = dsncc;
            //cbNhaCungCap.DisplayMember = "TenNhaCungCap";
            //cbNhaCungCap.ValueMember = "MaNhaCungCap";

            Image img = Image.FromFile("Images/NoImage.jpg");
            lbHinhAnh.BackgroundImage = img;
        }


        private void lbHinhAnh_Click(object sender, EventArgs e)
        {
            OpenFileDialog op = new OpenFileDialog();
            op.InitialDirectory = "D:";
            op.Filter = ".jpg, .png, .jpeg|*.jpg; *.png; *.jpeg";
            if (op.ShowDialog() == DialogResult.OK)
            {
                PathFile = op.FileName;
                Image img = Image.FromFile(op.FileName);
                lbHinhAnh.BackgroundImage = img;
                //chi lay ten anh
                TenFile = op.FileName.Substring(op.FileName.LastIndexOf("\\") + 1, op.FileName.Length - op.FileName.LastIndexOf("\\") - 1);
            }
        }

        private void lbHinhAnh_MouseHover(object sender, EventArgs e)
        {
            lbHinhAnh.BorderType = eBorderType.Etched;
        }

        private void lbHinhAnh_MouseLeave(object sender, EventArgs e)
        {
            lbHinhAnh.BorderType = eBorderType.None;
        }

        private void txtGiaNhap_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar))
            {
                e.Handled = true;
            }
        }

        private void txtGiaBan_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar))
            {
                e.Handled = true;
            }
        }

        private void txtSoLuong_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar))
            {
                e.Handled = true;
            }
        }

        private void txtGiaNhap_Leave(object sender, EventArgs e)
        {
            if (txtGiaNhap.Text == "")
                txtGiaNhap.Text = "0";
            else
            {
                try
                {
                    txtGiaNhap.Text = string.Format("{0:#,0.#}", Double.Parse(txtGiaNhap.Text));
                }
                catch { }
            }
        }

        private void txtGiaBan_Leave(object sender, EventArgs e)
        {
            if (txtGiaBan.Text == "")
                txtGiaBan.Text = "0";
            else
            {
                try
                {
                    txtGiaBan.Text = string.Format("{0:#,0.#}", Double.Parse(txtGiaBan.Text));
                }
                catch { }
            }
        }

        private void txtGiaNhap_Enter(object sender, EventArgs e)
        {
            txtGiaNhap.Text = txtGiaNhap.Text.Replace(",", "");
        }

        private void txtGiaBan_Enter(object sender, EventArgs e)
        {
            txtGiaBan.Text = txtGiaBan.Text.Replace(",", "");
        }
    }
}